package com.jiangyg.devtools.ui.table.cell;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.ui.components.JBPanel;
import com.jiangyg.devtools.DevtoolsException;
import com.jiangyg.devtools.ui.IconLabel;
import com.jiangyg.devtools.ui.event.ClickButtonEvent;
import com.jiangyg.devtools.ui.table.AbstractEditableTableModel;
import com.jiangyg.devtools.toolkit.ReflectionUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

/**
 * 类描述：操作单元格编辑器
 *
 * @author jiangyg
 * @date 2023-08-31
 */
public class OperationalCellEditor extends AbstractTableCellEditor {

    private Object value;

    private JTable table;

    private int rowIndex;

    private JBPanel<JBPanel> toolbar;

    public OperationalCellEditor(Project project) {
        super(project);

        initToolbar();
    }

    private void initToolbar() {
        toolbar = new JBPanel<>(new FlowLayout());
        final IconLabel addLabel = new IconLabel("add");
        final IconLabel deleteLabel = new IconLabel("delete");

        toolbar.add(addLabel);
        toolbar.add(deleteLabel);

        addLabel.addMouseListener(new ClickButtonEvent() {

            @Override
            public void doClick(MouseEvent event) {
                try {
                    final AbstractEditableTableModel<Object> model = (AbstractEditableTableModel) table.getModel();

                    final Class<?> clazz = ReflectionUtils.getSuperClassGenericType(model.getClass(), 0);
                    final Object data = clazz.getConstructor().newInstance();

                    stopCellEditing();
                    model.addRow(rowIndex + 1, data);
                } catch (Exception ex) {
                    throw new DevtoolsException("新增数据行出错", ex);
                }
            }

        });

        deleteLabel.addMouseListener(new ClickButtonEvent() {

            @Override
            public void doClick(MouseEvent event) {
                final AbstractEditableTableModel<Object> model = (AbstractEditableTableModel) table.getModel();
                final boolean ask = MessageDialogBuilder
                        .okCancel("操作提示", "确认要删除选中行数据？")
                        .yesText("确认").noText("取消")
                        .ask(project);
                if (ask) {
                    stopCellEditing();
                    model.removeRow(rowIndex);
                }
            }

        });

    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 int rowIndex,
                                                 int columnIndex) {
        this.value = value;
        this.table = table;
        this.rowIndex = rowIndex;
        return toolbar;
    }

    @Override
    public Object getCellEditorValue() {
        return value;
    }

}
